#include <QCoreApplication>
#include<iostream>
#include<QString>
#include<string>
#include<QTextStream>

using namespace std;

QString encrypt(QString plainText, QString key); //加密函数
QString decrypt(QString cipherText,QString key);//解密函数

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString plaintext="important",cipher,detext;
    QString key="123456789"; //密钥
    cout<<"plaintext is:"<<plaintext.toStdString()<<endl<<"key is:"<<key.toStdString()<<endl;
    cipher=encrypt(plaintext,key);  //函数加密
    cout<<"cipher is:"<<cipher.toStdString()<<endl;
    detext=decrypt(cipher,key);     //函数解密
    cout<<"decrypt text is:"<<detext.toStdString()<<endl;

    return a.exec();
}



QString encrypt(QString plainText, QString key)
{
    string text =plainText.toStdString();//接收明文
    string keystr=key.toStdString();//接收密钥
    QString ciphertext;
    int length =(int)text.size(); //获取密文长度
    for(int i=0;i<length;i++) //异或
    {
        ciphertext[i]=text[i]^keystr[i];
    }
    return ciphertext;
}

QString decrypt(QString cipherText,QString key)
{
    string text=cipherText.toStdString();//接收密文
    string keystr =key.toStdString();
    QString plainText;
    int length =(int)text.size(); //获取密文长度
    for(int i=0;i<length;i++)
    {
        plainText[i]=text[i]^keystr[i];
    }
    return  plainText;
}
